<?php

require_once "PB.php";

	class ProfesorModel extends PB{
		
		public function getProfesorById($id) {
			$this->conn();
			
			
			$profesor = $this->read('profesor','*',array("idprofesor" => $id),null,false);
			
			//echo "SEM V modelu, oseba: <br>";
			//print_r($oseba);
			
			//$uci = $this->read('oseba_profesor','oseba_idoseba',array("profesor_idprofesor" => $profesor->getIdprofesor()),null,true);
			//echo "SEM V modelu, iscem ucence za prof: <br>";
			//print_r($uci);
			
			
			$sql = "SELECT * FROM oseba_profesor WHERE profesor_idprofesor = :idprof";
			$pb = $this->getConnection();
			$stmt = $pb->prepare($sql);
			$stmt->bindValue(":idprof", $profesor->getIdprofesor());
			$stmt->execute();
			
			$ucenci = array();
			$i = 0;
			
			while($ucenec = $stmt->fetch()){
				//echo "sem v WHILE loop... $i ... oseba:<br>";
				//print_r($ucenec);
				$ucenci[$i] = $this->read('oseba','*',array("idoseba" => $ucenec["oseba_idoseba"]));
				$i++;
			}
			
			
						
			//$prijavniPod = $this->read('prijavnipodatki','*', array("idprijavniPodatki" => $profesor->getPrijavniPodatki_idprijavnipodatki()));
			//echo "SEM v modelu, iscem prijavne podatke za osebo: <br>";
			//print_r($prijavniPod);
			
			$profesor->setUci($ucenci);
			//$profesor[0]->setPrijavniPodatki_idprijavnipodatki($prijavniPod[0]);	

			$this->disconnect();
			return $profesor;
		}
        
        public function getAllProfesor() {
			$this->conn();
			
			
			$profesor = $this->read('profesor','*',null,null,true);
			
			//echo "SEM V modelu, oseba: <br>";
			//print_r($oseba);
			
			//$uci = $this->read('oseba_profesor','oseba_idoseba',array("profesor_idprofesor" => $profesor->getIdprofesor()),null,true);
			//echo "SEM V modelu, iscem ucence za prof: <br>";
			//print_r($uci);
			
			
			$sql = "SELECT * FROM profesor";
			$pb = $this->getConnection();
			$stmt = $pb->prepare($sql);
			
			$stmt->execute();
			
			$this->disconnect();
			return $profesor;
		}
        
        public function removeProfesor($id) {
			$this->conn();
			
			
			$profesor = $this->delete('profesor',$id);
			
			//echo "SEM V modelu, oseba: <br>";
			//print_r($oseba);
			
			//$uci = $this->read('oseba_profesor','oseba_idoseba',array("profesor_idprofesor" => $profesor->getIdprofesor()),null,true);
			//echo "SEM V modelu, iscem ucence za prof: <br>";
			//print_r($uci);
			
			
			//$sql = "DELETE FROM profesor WHERE idprofesor=";
			//$pb = $this->getConnection();
			//$stmt = $pb->prepare($sql);
			
			//$stmt->execute();
			
			$this->disconnect();
			return $profesor;
		}
        
        public function registerProfesor($data) {
			
			//TODO: preveri pravilnost izpolnjevanja
			//echo " DATA ARRAY: <br>";
			//print_r($data);
			
			
			$upIme = $data["upIme"];
			$data["geslo"] = md5($data["geslo"]);
			//$tip = $data["tip"];
			
			//TODO: precisti podatke
			$credentials = $this->array_slice_assoc($data, 'upIme', 2,true);
			//echo "<br> osebni podatki: <br>";
            $credentials['tip'] = "profesor";
			//print_r($credentials);
			$this->conn();
			$id = $this->write('prijavnipodatki', $credentials);
			//echo "<br>sem v register metodi: ".$id." <br> preostali ARRAY: ";
			
			$data = $this->array_slice_assoc($data, 'ime', 2);
			$data["prijavniPodatki_idprijavniPodatki"] = $id;
            
			//print_r($data);
			//$oseba = new Oseba();
			//$oseba->setIme($data["ime"]);
			$this->write('profesor',$data);
			
			return $data;
		}
		
		
		public function getUcenciPredmeti($id) {
			$this->conn();
						
			//$profesor = $this->read('profesor','*',array('idprofesor'=>$id),null,false);
			//echo "profesor model: <br>";
			//print_r($profesor);
			$sql = "SELECT * FROM oseba_profesor WHERE profesor_idprofesor = :idprof";
			$pb = $this->getConnection();
			$stmt = $pb->prepare($sql);
			$stmt->bindValue(":idprof", $id);
			$stmt->execute();
			
			$ucenciPredmeti = array();
			//$i = 0;
			
			
			$predmeti = $stmt->fetchAll(PDO::FETCH_ASSOC);
			$ucenciPredmeti = $predmeti;
			
			
			for($i=0;$i<count($predmeti);$i++){
				$ucenec = $this->read('oseba','*',array("idoseba"=>$predmeti[$i]["oseba_idoseba"]));
				$ucenciPredmeti[$i]["ucenec"] = $ucenec;
			}
			
			$this->disconnect();
			return $ucenciPredmeti;
		}
		
		
		public function oceniStudenta($idStud,$ocena,$predmet){
			$this->conn();
			
			$this->update('oseba_profesor',$ocena,array("oseba_idoseba" => $idStud,"predmet" => $predmet));
			
		}
        
	}
?>
